/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.Account;
import model.GroupAccount;
import model.SubGroupAccount;

import org.apache.log4j.Logger;

/**
 *
 * @author adriano.lourenco
 */
public class PlanAccountDAO implements DAO {

	final static Logger logger = Logger.getLogger(PlanAccountDAO.class);

	private final DataSource dataSource;

	public PlanAccountDAO() {
		dataSource = new DataSource();
	}

	public void create(Object o) {
		throw new UnsupportedOperationException("Not supported yet."); 
	}

	public void delete(Object o) {
		throw new UnsupportedOperationException("Not supported yet."); 	}

	public void update(Object o) {
		throw new UnsupportedOperationException("Not supported yet."); 
	}

	public Object read(Object o) {
		throw new UnsupportedOperationException("Not supported yet."); 	}

	public Object findAll() {
		try {
			String sql = "SELECT * FROM groupaccount";

			PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
			ArrayList<GroupAccount> listaGrupoContas = new ArrayList<GroupAccount>();
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {

				GroupAccount ga = new GroupAccount();

				ga.setIdgroupaccount(rs.getString("GroupAccount.idgroupaccount"));
				ga.setLstGrupoDeContas(rs.getString("GroupAccount.descricao"));

				listaGrupoContas.add(ga);

			}
			return listaGrupoContas;

		} catch (Exception e) {

			logger.error("GROUPACCOUNT.READ - ", e);

			return null;
		}

	}

	public Object findPlans() {

		try {
			String sql = "SELECT * FROM planAccount  inner join groupaccount "
						+ "on planAccount.groupaccount = groupaccount.idgroupaccount "
						+"inner join subgroupaccount "
						+ "on (planAccount.subgroupaccount = subgroupaccount.id)";
					
			
		
			PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();

			ArrayList<Account> listPlans = new ArrayList<Account>();
			
			while (rs.next()) {

				Account ac = new Account();
				
				ac.setLstPlanoDeContas(rs.getString("planAccount.descricao"));
				ac.setTxtDescricao(rs.getString("planAccount.desc_extendida"));
				ac.setLstStatus(rs.getString("planAccount.status"));

				GroupAccount ga = new GroupAccount();
				ga.setLstGrupoDeContas(rs.getString("groupaccount.descricao"));
				
				ac.setGroupaccount(ga);
				
				
				SubGroupAccount sb = new SubGroupAccount();
				sb.setDescricao(rs.getString("subgroupaccount.descricao"));
				
				ac.setSubgroupaccount(sb);
				
				listPlans.add(ac);

			}
			return listPlans;

		} catch (SQLException e) {

			logger.error("AllPlans ", e);
			return null;
			
		}

	}

	public void closeDAO() {
		dataSource.closeConnection();
	}

}
